package com.lzk.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lzk.JsonResult;
import com.lzk.domain.Product;
import com.lzk.domain.Store;
import com.lzk.product.openfeignClients.ProductFeignClient;
import com.lzk.product.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private ProductFeignClient productFeignClient;

    @PostMapping
    public JsonResult save(@RequestBody Product product){
        return productService.save(product);
    }

    /**
     * 接收前端传来的三个参数
     *  “name” 商品名称
     *   “price” 商品价格
     *   “number” 商品数量
     * @param product
     * @return
     */
    @PostMapping("add")
    @SentinelResource(value = "store")
    public JsonResult add(@RequestBody Product product){
        productService.save(product);
        Store store = new Store();
        store.setProduct_id(product.getId());
        store.setNumber(product.getNumber());
        return productFeignClient.save(store);
    }
}
